﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cinemon;
using Cinemon.MySqlConnect;
using MySql.Data.MySqlClient;
using System.Data;
using System.Drawing;

public partial class UserRegistration : BaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PopulateSecurityQuestions();
    }

    protected void btnRegister_Click(object sender, EventArgs e)
    {
        try
        {
            DatabaseManager dbm = new DatabaseManager();
            List<MySqlParameter> sqlParams = new List<MySqlParameter>();
            sqlParams.Add(new MySqlParameter("user_username", tbxUsername.Text));
            DataTable dtUserName = dbm.ExecuteQuery("validateNewUsername", sqlParams);

            if (dtUserName == null || dtUserName.Rows.Count == 0)
            {
                sqlParams = new List<MySqlParameter>();
                sqlParams.Add(new MySqlParameter("strUsername", tbxUsername.Text));
                sqlParams.Add(new MySqlParameter("strEmailAddress", tbxEmailAddress.Text));
                sqlParams.Add(new MySqlParameter("strPassword", tbxPassword.Text));
                sqlParams.Add(new MySqlParameter("intSecurityQuestionID", ddlSecurityQuestion.SelectedValue));
                sqlParams.Add(new MySqlParameter("strAnswer", tbxAnswer.Text));
                sqlParams.Add(new MySqlParameter("strFirstName", tbxFirstName.Text));
                sqlParams.Add(new MySqlParameter("strSurname", tbxSurname.Text));
                sqlParams.Add(new MySqlParameter("strCellNo", tbxCellNumber.Text));
                sqlParams.Add(new MySqlParameter("strAddress", tbxHomeAddress.Text));

                DataTable dtSecQuest = dbm.ExecuteQuery("InsertUser", sqlParams);

                if (dtSecQuest != null && dtSecQuest.Rows.Count > 0 && Convert.ToInt32(dtSecQuest.Rows[0]["RowsCount"]) > 0)
                {
                    lblRegisterStatus.Text = "You are now registered!";
                    lblRegisterStatus.ForeColor = Color.Green;
                }
                else
                {
                    lblRegisterStatus.Text = "An error has occured.";
                    lblRegisterStatus.ForeColor = Color.Red;
                }
            }
            else
            {
                lblRegisterStatus.Text = "Username not available.";
                lblRegisterStatus.ForeColor = Color.Red;
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.ToString();
            LogLibrary.EventLogger.Log.LogError(ex);
        }
    }

    protected void PopulateSecurityQuestions()
    {
        try
        {
            DatabaseManager dbm = new DatabaseManager();
            List<MySqlParameter> sqlParams = new List<MySqlParameter>();

            DataTable dtSecQuest = dbm.ExecuteQuery("GetSecurityQuestions", sqlParams);

            ddlSecurityQuestion.DataTextField = "Question";
            ddlSecurityQuestion.DataValueField = "ID";
            ddlSecurityQuestion.DataSource = dtSecQuest;
            ddlSecurityQuestion.DataBind();
        }
        catch (Exception ex)
        {
            lblError.Text = ex.ToString();
            LogLibrary.EventLogger.Log.LogError(ex);
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "ReturnToLogin", "window.location = 'Default.aspx';", true);
    }
}